package com.dzpk.gameserver.util;

/**
 * 生成一个19位的全局唯一的long型ID
 * 
 * @author admin
 * 
 */
public class IDGenerator {

    // 12位
    private long timeTemplate;

    // 3位
    private int order;

    // 4位
    private String serverId;

    public void init() {
        order = 0;
        timeTemplate = System.currentTimeMillis() / 10;
    }

    private void reset() {
        order = 0;
        timeTemplate++;
    }

    public long generateID() {
        if (++order > 999) {
            reset();
        }
        return Long.parseLong(serverId + timeTemplate + getOrder());
    }

    private String getOrder() {
        if (order < 10) {
            return "00" + order;
        } else if (order < 100) {
            return "0" + order;
        }
        return order + "";
    }

    public void setServerId(String serverId) {
        this.serverId = serverId.trim();
    }
}
